<template>
	<view>
		<view class="list">
			<template v-if="list.length > 0">
				<view class="complist">
					<view class="index_sale">
						<view class="free-WaterfallFlow">
							<view class="flex-wrap" v-for="(item,index) in list" :key="index" @click="imageClick(index)"
								:class="{'flex-filter': isDownload && !item.checked}">
								<view class="page-column">
									<image mode="widthFix" :src="item.image"></image>
								</view>

								<view class="check-img" v-if="isDownload">
									<image
										:src="item.checked ? '../../../../static/icon/method_checked.png' :'../../../../static/icon/method_check.png'"
										mode=""></image>
								</view>
							</view>
						</view>
					</view>

					<view class="footer">
						<view class="confirm-btn" @click="downLoadClick" v-if="!isDownload">
							下载图片
						</view>
						<view class="footer-content" v-else>
							<view class="footer-left">
								<view class="ftooer-btn btn-white" @click="cancelDownload">
									取消
								</view>
							</view>
							<view class="ftooer-right">
								<view class="ftooer-btn btn-black" @click="confirmDownload">
									确认({{downloadNums}})
								</view>
							</view>
						</view>
					</view>
				</view>
			</template>
			<template v-else>
				<view class="empty" v-if="show">
					<view class="empty-text">
						暂时没有数据
					</view>
				</view>
			</template>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				id: 0,
				list: [],
				previewList: [],
				show: false,
				isDownload: false,
				downloadNums: 0
			}
		},
		onLoad(options) {
			this.id = options.id

			this.getImages()
		},
		methods: {
			getImages() {
				let _this = this

				uni.showLoading({
					title: '加载中...',
					mask: true
				})

				_this.$api.get('meet.order/orderImages', {
					order_id: _this.id
				}).then(res => {
					uni.hideLoading()
					if (res.code == 1) {
						_this.list = res.data.list.map((item, index) => {
							item.checked = false
							return item
						})
						_this.previewList = _this.list.map((item, index) => {
							return item.image
						})

						_this.show = true
					} else {
						uni.showModal({
							title: '提示',
							content: res.msg,
							showCancel: false,
							cancelText: '',
							confirmText: '确认',
							success: res => {
								uni.navigateBack({
									delta: 1
								});
							},
							fail: () => {},
							complete: () => {}
						});
					}
				}, fail => {})
			},
			imageClick(index) {
				if (this.isDownload) {
					this.checkImage(index)
				} else {
					this.previewClick(index)
				}
			},
			previewClick(index) {
				uni.previewImage({
					current: index,
					urls: this.previewList
				})
			},
			checkImage(index) {
				let _this = this

				let downloadNums = 0

				if (!_this.list[index].checked) {
					for (let i = 0; i < _this.list.length; i++) {
						if (_this.list[i].checked) {
							downloadNums++
						}

						if (downloadNums >= 9) {
							_this.toast('最多可选9张')
							return false
						}
					}
				}

				downloadNums = 0

				_this.list[index].checked = !_this.list[index].checked

				for (let i = 0; i < _this.list.length; i++) {
					if (_this.list[i].checked) {
						downloadNums++
					}
				}

				_this.downloadNums = downloadNums
			},
			downLoadClick() {
				this.isDownload = true
			},
			cancelDownload() {
				let _this = this
				_this.isDownload = false
				_this.downloadNums = 0
				for (let i = 0; i < _this.list.length; i++) {
					_this.list[i].checked = false
				}
			},
			confirmDownload() {
				let _this = this
				if (_this.downloadNums <= 0) {
					_this.toast('最少选择1张图片')
					return false
				}

				let imageList = []

				for (let i = 0; i < _this.list.length; i++) {
					if (_this.list[i].checked) {
						imageList.push(_this.list[i].image)
					}
				}

				uni.showModal({
					title: '提示',
					content: '确认下载当前已选择的图片吗？',
					showCancel: true,
					cancelText: '取消',
					confirmText: '确认',
					success: res => {

						if (res.confirm) {
							_this.downloadImages(imageList)
						}
					},
					fail: () => {},
					complete: () => {}
				});
			},
			downloadImages(imageList) {
				let _this = this
				let length = imageList.length
				let nums = 0

				uni.showLoading({
					title: '下载中...',
					// mask: true
				})

				for (let i = 0; i < length; i++) {
					uni.downloadFile({
						url: imageList[i],
						success(res) {
							uni.saveImageToPhotosAlbum({
								filePath: res.tempFilePath,
								success() {
									
								},
								complete(res) {		
									if (nums == length - 1) {
										uni.hideLoading()
										_this.toast('下载成功')
									} else {
										nums++
									}
								}
							})

						},
						fail() {
							if (nums == length - 1) {
								uni.hideLoading()
							} else {
								nums++
							}
						}
					})
				}
			}
		}
	}
</script>

<style lang="scss">
	page {
		background-color: #F6F6F6;
		padding-bottom: 120rpx;
	}

	.list {
		padding-top: 10px;
		padding-bottom: 20px;
	}

	.empty {
		width: 100%;
		text-align: center;
		margin-top: 40%;

		image {
			width: 200rpx;
			height: 200rpx;
		}

		.empty-text {
			font-size: 32rpx;
			color: #999999;
			margin-top: 80rpx;
		}
	}

	.complist {
		margin-top: 10px;
		padding: 0 15px;

		.index_sale {
			.free-WaterfallFlow {
				width: 100%;
				// column-count: 2;

				/*column-count 属性规定元素应该被分隔的列数：*/
				.flex-wrap {
					display: inline-block;
					width: 50%;
					// margin-right: 2%;
					text-align: left;
					vertical-align: top;
					margin-bottom: 24rpx;
					position: relative;

					.page-column {
						width: 100%;

						image {
							width: 97%;
							margin: 0 auto;
							border-radius: 14rpx;
						}
					}

					.check-img {
						position: absolute;
						top: 10rpx;
						right: 16rpx;

						image {
							width: 40rpx;
							height: 40rpx;
						}
					}
				}

				.flex-filter {
					filter: grayscale(30%);
				}

				.flex-wrap:nth-child(2n) {
					text-align: right;
				}
			}
		}
	}

	.footer {
		width: 100%;
		position: fixed;
		bottom: 0;
		left: 0;
		display: flex;
		align-items: center;
		justify-content: center;
		background-color: #FFFFFF;
		padding: 30rpx 0;
		box-shadow: 0rpx 6rpx 12rpx 2rpx rgba(0, 0, 0, 0.5);

		.confirm-btn {
			width: 80%;
			background: #000000;
			border-radius: 50rpx;
			font-weight: 800;
			font-size: 28rpx;
			color: #FFFFFF;
			padding: 20rpx 0;
			text-align: center;
		}

		.footer-content {
			width: 100%;
			display: flex;
			align-items: center;
			justify-content: space-between;

			.footer-left {
				width: 50%;
				display: flex;
				justify-content: center;
			}

			.ftooer-right {
				width: 50%;
				display: flex;
				justify-content: center;
			}

			.ftooer-btn {
				width: 80%;
				border-radius: 40rpx;
				font-weight: 800;
				font-size: 28rpx;
				display: flex;
				align-items: center;
				justify-content: center;
				padding: 20rpx 0;
			}

			.btn-white {
				background: #FFFFFF;
				color: #000000;
				border: 2rpx solid #000000;
			}

			.btn-black {
				background-color: #000000;
				color: #FFFFFF;
			}
		}
	}
</style>